#include "myFunctions.h"
#include "bpNet.h"
int main(int argc,char *argv[]){
	bpNet Network;

	float **vRand;
	float **targets;
	vector<float> output;
	float error;
	mdouble datosa, datosb,X;
	int numPatterns = 1000;
	vector<int> y(numPatterns),yd;
  
	genRandom("xor.txt", "xorRandomTest.txt",100, numPatterns);
	readFile(X,yd,"xorRandomTest.txt",true);

	vRand = (float**)calloc(numPatterns,sizeof(float *));
	targets = (float **)calloc(numPatterns,sizeof(float*));
	for (int i=0; i<numPatterns ; i++){
		vRand[i] = (float *)calloc(2,sizeof(float));
		targets[i] = (float *)calloc(1,sizeof(float));
	}     

	for (int i=0;i<numPatterns;i++){
		vRand[i][0] = X[i][1];
		vRand[i][1] = X[i][2];
		targets[i][0] = yd[i];
		//cout<<vRand[i][0]<<", "<<vRand[i][1]<<", "<<targets[i][0]<<endl;
	}

	Network.Load("xorNet.txt");
	
	output.resize(numPatterns);
	
	for (int i = 0; i < numPatterns; i++){
		output[i] = *(Network.getOutput(vRand[i]));  
    	vector<double> aux;    
		aux.push_back(vRand[i][0]);
		aux.push_back(vRand[i][1]);
		//cout<<"output:"<<output[i]<<endl;
		if(output[i]>0){
			error += fabs(targets[i][0]-1);
			datosa.push_back(aux);
		}else{
			datosb.push_back(aux);
			error += fabs(targets[i][0]-(-1));
		}
		
  	}
	
	cout<<"Error de Red: "<<error/numPatterns<<endl;
	saveFile(datosa,y,"xor_class_1.txt",false);
	saveFile(datosb,y,"xor_class_2.txt",false);
	
	Network.printData("xor_class_1.txt", "xor_class_2.txt");
	
	return 1;
}
